package edu.xpu.project.vehicle.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.xpu.project.user.entity.DriverEntity;
import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.user.service.DriverEntityService;
import edu.xpu.project.vehicle.entity.SchoolBus;
import edu.xpu.project.vehicle.service.SchoolBusService;

/**
 * @ClassName: SchoolBusController.java
 * @Description: 校车信息控制器
 *
 * @author liukang
 * @date 2017年5月5日 下午2:51:04
 * @version 
 * @since JDK 1.6
 */
@Controller
@RequestMapping(value="/schoolbus")
public class SchoolBusController {
	
	@Autowired
	private SchoolBusService schoolBusService;
	@Autowired
	private DriverEntityService driverEntityService;
	
	/**
	  * @MethodName: assignDriver
	  * @Description: 为校车指派司机
	  * @Author liukang
	  * @date 2017年5月26日  上午11:12:27
	 */
	@ResponseBody
	@RequestMapping(value="assignDriver/{busId}/{driverId}")
	public String assignDriver(@PathVariable(value="busId") Long busId,
			@PathVariable(value="driverId") Long driverId) {
		SchoolBus schoolBus = schoolBusService.get(busId);
		DriverEntity driverEntity = driverEntityService.get(driverId);
		if (driverEntity != null && schoolBus != null) {
			schoolBus.setDriverEntity(driverEntity);
			SchoolBus saveOrUpdate = schoolBusService.saveOrUpdate(schoolBus);
			if (saveOrUpdate != null) {
				return "{\"status\":\"success\"}";
			}
		}
		return "{\"status\":\"error\"}";
	}
	
	/**
	  * @MethodName: startUpBus
	  * @Description: 激活校车
	  * @Author liukang
	  * @date 2017年5月23日  下午4:53:49
	 */
	@ResponseBody
	@RequestMapping(value="startUpBus/{id}")
	public String startUpBus(@PathVariable(value="id") Long id) {
		SchoolBus schoolBus = schoolBusService.get(id);
		if (schoolBus != null) {
			schoolBus.setStatus(1);
			schoolBusService.saveOrUpdate(schoolBus);
			return "{\"status\":\"success\"}";
		}
		return "{\"status\":\"error\"}";
	}
	
	/**
	  * @MethodName: schoolbusDetails
	  * @Description: 校车详情
	  * @Author liukang
	  * @date 2017年5月23日  下午12:17:54
	 */
	@RequestMapping(value="schoolbusDetails/{id}")
	public String schoolbusDetails(@PathVariable(value="id") Long id, ModelMap modelMap) {
		SchoolBus schoolBus = schoolBusService.get(id);
		modelMap.addAttribute("schoolBus", schoolBus);
		return "bus/schoolBusDetail";
	}
	
	/**
	 * @describe 校车删除
	 * @date 2017年5月23日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="schoolbusDel/{id}")
	public String schoolbusDel(@PathVariable(value="id") Long id) {
		try {
			schoolBusService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"success\"}";
	}
	
	/**
	 * @describe 根据校车状态获取校车信息
	 * @date 2017年5月19日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="getSchoolBusByStatus/{status}", produces={"application/json; charset=utf-8"})
	public String getSchoolBusByStatus(@PathVariable(value="status") Integer status) {
		List<SchoolBus> schoolBusByStatus = schoolBusService.getSchoolBusByStatus(status);
		ObjectMapper objectMapper = new ObjectMapper();
		String schoolbusJson = "1";
		try {
			schoolbusJson= objectMapper.writeValueAsString(schoolBusByStatus);
		} catch (IOException e) {}
		return schoolbusJson;
	}
	
	/**
	 * @describe 校车添加
	 * @date 2017年5月19日
	 * @author liukang
	 */
	@RequestMapping(value="/schoolbusAdd")
	public String schoolbusAdd(SchoolBus schoolBus, @ModelAttribute("sessionUser") ProjectUser sessionUser) {
		SchoolBus saveObj = null;
		if (schoolBus.getId() == null) {
			schoolBus.setCreateDate(new Date());
			schoolBus.setCreatePersonId(sessionUser.getId());
			schoolBus.setCreatePersonName(sessionUser.getName());
			saveObj = schoolBus;
		} else {
			SchoolBus schoolBus2 = schoolBusService.get(schoolBus.getId());
			schoolBus2.setName(schoolBus.getName());
			schoolBus2.setSchoolNo(schoolBus.getSchoolNo());
			schoolBus2.setSeatTotal(schoolBus.getSeatTotal());
			schoolBus2.setStatus(schoolBus.getStatus());
			schoolBus2.setLocalStatus(schoolBus.getLocalStatus());
			saveObj = schoolBus2;
		}
		schoolBusService.saveOrUpdate(saveObj);
		return "redirect:schoolBusList";
	}
	
	/**
	  * @MethodName: toSchoolBusAdd
	  * @Description: 去校车添加界面
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年5月5日  下午2:56:43
	 */
	@RequestMapping(value="toSchoolBusAdd")
	public String toSchoolBusAdd() {
		
		return "bus/schoolBusAdd";
	}
	
	/**
	  * @MethodName: schoolBusList
	  * @Description: 校车列表
	  * @param pageNumber
	  * @param pageSize
	  * @param schoolBus
	  * @param modelMap
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年5月5日  下午2:55:08
	 */
	@RequestMapping(value="schoolBusList")
	public String schoolBusList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			SchoolBus schoolBus, ModelMap modelMap) {
		Page<SchoolBus> pageByCriterias = schoolBusService.getPageByCriterias(pageNumber, pageSize, schoolBus);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize)
				.addAttribute("searchVal", schoolBus);
		//查找所有司机
		List<DriverEntity> findAll = driverEntityService.findAll();
		modelMap.addAttribute("drivers", findAll);
		return "bus/schoolBusList";
	}

}

